function artifactRejection(subject, allTrials, condition)
    % Visual artifact rejection. For detection of jumps, muscle, eyetracker and EOG artifacts. Cannot be run in Torque due to user intervention required
	% subject: struct with subject specific information. Should at least
	% contain the fields subjID, subjectDir, MEGChannels (all MEG channels
	% but the rejected ones), MRTChannels (all MRT channels but the
	% rejected ones) and resampledData (path to the raw dataset)
	% allTrials: defined trials
	% Stores artifacts and the corresponding trials in subject.subjectDir/subject.subjID_cfg_rejectArtifact.mat
	
	%% Jump detection
    
    artifacts = [];
    
    cfg = [];
    cfg.trl = allTrials;
    cfg.continuous = 'yes';
    cfg.artfctdef.zvalue.interactive = 'yes';
    cfg.headerfile = subject.resampledData;
    cfg.datafile = subject.resampledData;
    
    cfg.artfctdef.zvalue.channel = subject.MEGChannels;
    cfg.artfctdef.zvalue.cutoff = 30;
    cfg.artfctdef.zvalue.trlpadding = .1;
    cfg.artfctdef.zvalue.artpadding = .1;
    cfg.artfctdef.zvalue.fltpadding = .1;
    cfg.artfctdef.zvalue.detrend = 'yes';
    
    cfg.artfctdef.zvalue.medianfilter = 'yes';
    cfg.artfctdef.zvalue.medianfiltord = 9;
    cfg.artfctdef.zvalue.absdiff = 'yes';
    
    [cfg, artifacts.jump] = ft_artifact_zvalue(cfg);    
        
    %% automatic muscle detection

    cfg                                 = [];
    cfg.trl                             = allTrials;
    cfg.continuous                      = 'yes';
    cfg.datafile                        = subject.resampledData;
    cfg.headerfile                      = subject.resampledData;
 
    cfg.artfctdef.zvalue.channel        = subject.MRTChannels;
    cfg.artfctdef.zvalue.cutoff         = 10;

    cfg.artfctdef.zvalue.trlpadding     = .1;
    cfg.artfctdef.zvalue.artpadding     = .1;
    cfg.artfctdef.zvalue.fltpadding     = .1;

    cfg.artfctdef.zvalue.bpfilter       = 'yes';
    cfg.artfctdef.zvalue.bpfreq         = [110 140];
    cfg.artfctdef.zvalue.bpfiltord      = 9;
    cfg.artfctdef.zvalue.bpfilttype     = 'but';
    cfg.artfctdef.zvalue.hilbert        = 'yes';
    cfg.artfctdef.zvalue.boxcar         = 0.2;

    cfg.artfctdef.zvalue.interactive    = 'yes';

    [cfg, artifacts.muscle] = ft_artifact_zvalue(cfg);
    
    %% automatic eyetracker artifact detection
    if ~strcmp(subject.subjID, 'P01') % no eyetracker and EOG data for P01
        cfg                                 = [];
        cfg.trl                             = allTrials;
        cfg.continuous                      = 'yes';
        cfg.datafile                        = subject.resampledData;
        cfg.headerfile                      = subject.resampledData;

        cfg.artfctdef.zvalue.channel        = {'UADC005', 'UADC006'};
        cfg.artfctdef.zvalue.cutoff         = 4;

        cfg.artfctdef.zvalue.trlpadding     = .1;
        cfg.artfctdef.zvalue.artpadding     = .1;
        cfg.artfctdef.zvalue.fltpadding     = .1;

        cfg.artfctdef.zvalue.bpfilter       = 'yes';
        cfg.artfctdef.zvalue.bpfreq         = [1 15];
        cfg.artfctdef.zvalue.bpfiltord      = 4;
        cfg.artfctdef.zvalue.bpfilttype     = 'but';
        cfg.artfctdef.zvalue.hilbert        = 'yes';

        cfg.artfctdef.zvalue.interactive    = 'yes';

        [cfg, artifacts.eyetracker] = ft_artifact_zvalue(cfg);
    end
    
    %% automatic EOG artifact detection
    
    % for those few trials in which an artifact is detected in the EOG but
    % not the eyetracker signal.
   
    cfg                                 = [];
    cfg.trl                             = allTrials;
    cfg.continuous                      = 'yes';
    cfg.datafile                        = subject.resampledData;
    cfg.headerfile                      = subject.resampledData;
    
    if strcmp(subject.subjID, 'P01')
        cfg.artfctdef.zvalue.channel	=  {'MZF','MLF','MRF'}; %P01 has no EOG
    else
        cfg.artfctdef.zvalue.channel    = {'EEG057', 'EEG058'};
    end
    cfg.artfctdef.zvalue.cutoff         = 4;

    cfg.artfctdef.zvalue.trlpadding     = .1;
    cfg.artfctdef.zvalue.artpadding     = .1;
    cfg.artfctdef.zvalue.fltpadding     = .1;

    cfg.artfctdef.zvalue.bpfilter       = 'yes';
    cfg.artfctdef.zvalue.bpfreq         = [1 15];
    cfg.artfctdef.zvalue.bpfiltord      = 4;
    cfg.artfctdef.zvalue.bpfilttype     = 'but';
    cfg.artfctdef.zvalue.hilbert        = 'yes';

    cfg.artfctdef.zvalue.interactive    = 'yes';

    [cfg, artifacts.eog] = ft_artifact_zvalue(cfg);
    
	if ~strcmp(subject.subjID, 'P01') 
        % concatenate the two artifact matrices
        artifacts.eyemovs = sortrows([artifacts.eyetracker; artifacts.eog]);
	else
        artifacts.eyemovs = sortrows([artifacts.eog]); %P01 has no eyetracker data
	end
    %% reject artifacts from trial definition

    cfg = [];
    cfg.headerfile = subject.resampledData;
    cfg.trl = allTrials;
    cfg.artfctdef.reject = 'complete';
    cfg.artfctdef.jump.artifact = artifacts.jump;
    cfg.artfctdef.muscle.artifact = artifacts.muscle;
    cfg.artfctdef.eog.artifact = artifacts.eyemovs;

    cfg_rejectArtifact = ft_rejectartifact(cfg);

    save([subject.subjectDir, '/', subject.subjID, '_', condition, '_cfg_rejectArtifact.mat'],'cfg_rejectArtifact' ,'artifacts', '-v7.3') ;

end